from fastapi import FastAPI
from contextlib import asynccontextmanager
import logging
from app.core.request_middleware import RequestIDMiddleware
# 移除对不存在的processor_service的引用
from app.api.routes import router as api_router

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)




@asynccontextmanager
async def lifespan(app: FastAPI):
    # 启动时初始化（移除initialize_processors调用，处理器通过工厂函数动态创建）
    logger.info("应用启动中...")
    yield
    # 关闭时清理
    logger.info("应用关闭中...")


app = FastAPI(
    title="内容策略生成系统",
    version="1.0.0",
    lifespan=lifespan
)


app.add_middleware(RequestIDMiddleware)


from app.core.request_context import get_request_id

@app.get("/test-request-id")
async def test_request_id():
    request_id = get_request_id()
    return {
        "request_id": request_id,
        "message": "测试 request_id 传递"
    }

# 挂载路由
app.include_router(api_router, prefix="/api")


@app.get("/health")
async def health_check():
    return {"status": "healthy", "service": "content-strategy-api"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8843)